/*

	This file loads data from the Firm Accounts Statistics (abbreviated FIRE by DST)

*/

cd ${work}1_data

local start	2001
local end	2015
local list	"cvrnr oms iaat grby atam fmaa aadi lgag keb kene tdrt udhl hlul tgt ufkv udby"
local list1	"krh"
local list2	"krhe kvv"

*LOAD
foreach y of numlist `start'/`end' {

	use input\fire`y' if cvrnr!="" , clear
	
	if inrange(`y',`start',2003) 	keep `list' `list1'
	if inrange(`y',2004,`end')		keep `list' `list2'
	gen year=`y'
		
	save temp\fire_`y', replace	

}

clear
local command use
foreach y of numlist `start'/`end' {
	`command' temp\fire_`y'
	erase temp\fire_`y'.dta
	local command "append using"
}

*MODIFY
rename cvrnr jurnr

gen 	capital			= iaat+grby+atam+fmaa+aadi
gen 	expenditures	= krh
replace expenditures	= krhe+kvv if year>=2004
drop iaat grby atam aadi krh krhe kvv

rename	oms				revenue
rename	lgag			wagebill
rename	hlul			inventory
rename	keb				purchase_buildings
rename	kene			purchase_energy
rename	tdrt			purchase_opeq
rename	tgt				credit
rename	udby			dividends
rename	udhl			rent
rename	ufkv			prepayments

*SAVE
order jurnr year
sort jurnr year
compress
save output\fire_it, replace